<template>
  <div
    class="login-view"
    v-loading="true"
  />
</template>

<script>
export default {
  name: 'SsoLogin',
  data () {
    return {
      redirect: null
    }
  },
  watch: {
    $route: {
      handler (route) {
        this.redirect = route?.query?.redirect || '/'
      },
      immediate: true
    }
  },
  async created () {
    try {
      const { token } = await this.$request.post('/getCurrInfo')
      this.$store.commit('user/setToken', token)
      this.$nextTick(() => {
        this.$router.push(this.handleRoute())
      })
    } catch (error) {
      console.log(error)
      location.href = '/sso/login?back=' + encodeURIComponent(location.href)
    }
  },
  methods: {
    handleRoute () {
      return this.redirect === '/404' || this.redirect === '/403'
        ? '/'
        : this.redirect
    }
  }
}
</script>

<style lang="scss" scoped>
.login-view {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  background: rgb(247 244 244);
}
</style>
